package com.zrc.module_my.fragment

import android.content.Context
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ColorUtils
import com.zrc.superframe_base.ARouterHub
import com.zrc.superframe_base.mvx.mvc.BaseMvcFragment
import com.zrc.superframe_ui.layout.SuperViewPagerAdapter
import com.zrc.superframe_base.widget.magicindicator.ColorFlipPagerTitleView
import com.zrc.module_my.R
import kotlinx.android.synthetic.main.fragment_my.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
@Route(path = ARouterHub.FRAGMENT_MY, name = "我的")
class MyFragment : BaseMvcFragment() {

    lateinit var adapter: SuperViewPagerAdapter

    override fun getLayoutID() = R.layout.fragment_my

    override fun initListener() {
        super.initListener()
        adapter = SuperViewPagerAdapter(childFragmentManager)
        val netFragment = ARouter.getInstance().build(ARouterHub.FRAGMENT_NET).navigation() as BaseMvcFragment//网络
        val downFragment = ARouter.getInstance().build(ARouterHub.FRAGMENT_DOWN).navigation() as BaseMvcFragment//下载
        adapter.addFragment(netFragment, "网络", R.drawable.vector_error)
        adapter.addFragment(downFragment, "下载", R.drawable.vector_empty)
        view_pager.adapter=adapter
        initMagicIndicator1()
    }

    private fun initMagicIndicator1() {
        val commonNavigator = CommonNavigator(activity)
        //commonNavigator.scrollPivotX = 0.65f
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {

            override fun getCount(): Int=adapter.count

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView: SimplePagerTitleView =
                    ColorFlipPagerTitleView(
                        context
                    )
                simplePagerTitleView.text = adapter.getPageTitle(index)
                simplePagerTitleView.normalColor = ColorUtils.getColor(R.color.gray_707070)
                simplePagerTitleView.selectedColor =  ColorUtils.getColor(R.color.green_1afa29)
                simplePagerTitleView.setOnClickListener {
                    view_pager.currentItem = index
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineHeight = UIUtil.dip2px(context, 6.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 10.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                indicator.setColors(ColorUtils.getColor(R.color.green_1afa29))
                return indicator
            }
        }
        magicindicator.navigator = commonNavigator
        ViewPagerHelper.bind(magicindicator, view_pager)
    }
}